package BNS.wheat;

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;

/** Implements the GUI gadgets for setting and changing sales orders on the local market.
	*/
public class TradePanel extends JPanel implements ActionListener {
	
	private JLabel endowmentValue, acquiredValue;
	private JTextField newBuyVolume, newSellVolume;
	private JLabel remBuyVolume, remSellVolume, newBuyPrice, remBuyPrice;
	private JButton submit;
	private JPanel ctlPanel;
	private WheatMktPersona persona;
	
	/** Constructs and displays a TradePanel, which has fields for inputting (a selling volume
		and a buying volume), some error-checking routines, and various output fields.
		@param pers the BNSPersona that requested this panel (will be used to call in 
			an order).
		*/
	public TradePanel(WheatMktPersona pers) {
		super();
		setLayout(new BorderLayout());
		setBackground(WheatMktPersona.panelBackground);
		setOpaque(true);
		
		persona= pers;
		ctlPanel= new JPanel();
		
		JPanel pack1= new JPanel(new GridLayout(3,5));
													pack1.add(new JLabel("endowment  "));
		endowmentValue=	new JLabel("N/A yet");		pack1.add(endowmentValue);
													pack1.add(new JLabel("giving"));
		remSellVolume= new JLabel("N/A yet");		pack1.add(remSellVolume);
		newSellVolume= new JTextField(5);			pack1.add(newSellVolume);
		
													pack1.add(new JLabel(" acquired "));
		acquiredValue=	new JLabel("N/A yet");		pack1.add(acquiredValue);
													pack1.add(new JLabel("getting"));
		remBuyVolume= new JLabel("N/A yet");		pack1.add(remBuyVolume);
		newBuyVolume= new JTextField(5);			pack1.add(newBuyVolume);
		
		pack1.add(new JLabel(""));
													pack1.add(new JLabel(""));
													pack1.add(new JLabel("buyPrice"));
		remBuyPrice= new JLabel("N/A yet");			pack1.add(remBuyPrice);
		newBuyPrice= new JLabel(" ");				pack1.add(newBuyPrice);
		remBuyPrice.setForeground(Color.red);
		ctlPanel.add(pack1);
		
		submit= new JButton("submit");	submit.addActionListener(this);
		ctlPanel.add(submit);
		
		add(ctlPanel,BorderLayout.SOUTH);
		
		newBuyVolume.addActionListener(this);
		newSellVolume.addActionListener(this);
		validate();
		setLocation(new Point(200,200));
//		update();
		setVisible(true);
		repaint();
		}
	
	public static String form(double x) { return String.format("%7.3f",x); }
	
	public void update(Market market) {
		//System.err.println("updating TradePanel");
		endowmentValue.setText(form(market.endowment()));
		acquiredValue.setText(form(market.acquired()));
		if (market.myVolume() > 0) {
			remSellVolume.setText(form(market.myVolume()));
			remBuyVolume.setText(form(market.myVolume()*market.myPrice()));
			remBuyPrice.setText(form(1/market.myPrice()));
			}
		else {
			remSellVolume.setText("0");
			remBuyVolume.setText("0");
			remBuyPrice.setText("");
			}
		}
	
	public void actionPerformed(ActionEvent e) {
		Object src= e.getSource();
		if (src==submit) { 
			if (fieldsOK()) persona.order(sellingF,volumeF);
			}
		if (src==newSellVolume) { fieldsOK(); }
		if (src==newBuyVolume) { fieldsOK(); }
		}
	
	private Float volumeF, sellingF;
	private boolean fieldsOK() {
		volumeF=null; sellingF= null;
		sellingF= scan(newSellVolume);
		volumeF= scan(newBuyVolume);
		if (sellingF!=null && volumeF!=null) {
			System.err.println("developing order "+sellingF+" @ "+volumeF);
			newBuyPrice.setText(form(sellingF/volumeF));
			return true;
			}
		newBuyPrice.setText("");
		return false;
		}
	
	private Float scan(JTextField field) {
		try { String typed= field.getText().trim();
			if (typed.equals("")) { 
				field.setText(""); 
				return null; }
			Float val= Float.valueOf(typed); 
			if (val<0) {
				System.err.println("data entry error: "+field.getText()); 
				field.setText("negative"); }
			else return val; 
			} 
		catch (NumberFormatException e) { 
			System.err.println("data entry error: "+field.getText()); 
			field.setText("error"); }
		return null;
		}
	
	} // EOClass TradePanel
